package com.qf.conf;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

public class LoadBalancerConfig {

    @Bean
    public ReactorServiceInstanceLoadBalancer loadBalancer(Environment evn, LoadBalancerClientFactory factory){
        //获取当前负载均衡策略配置的服务名称
        String serviceName = evn.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);

        ObjectProvider<ServiceInstanceListSupplier> supplier = factory.getLazyProvider(serviceName,ServiceInstanceListSupplier.class);
        return new MyLoadBalancer(supplier,serviceName);
    }
}
